package com.kevin.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.nutz.dao.Condition;
import org.nutz.dao.QueryResult;
import org.nutz.ioc.loader.annotation.IocBean;

import com.kevin.model.Article;
import com.kevin.model.ArticleAttachment;
import com.kevin.model.ArticleFile;
import com.kevin.model.ArticleInfo;
import com.kevin.model.NavModel;
import com.kevin.validate.Errors;


/**
 * 
  * @ClassName: ArticleInfoService 
  * @Description: 文章
  * @author 吕凯
  * @date 2012-10-29 上午10:20:39 
  *
 */
@IocBean
public class ArticleInfoService extends BaseService{
	
	
	/**
	 * 
	  * @方法名  getArticleInfoList 
	  * @功能描述  获取所有文章 用于打印
	  * @author   吕凯  
	  * @param type
	  * @param currentPage
	  * @param pageSize
	  * @param request
	  * @param con 
	  * @return  void 
	  * @编码时间 2012-10-28 上午11:20:07
	 */
	public List<ArticleInfo> getArticleInfoList(HttpServletRequest request,Condition con) {
		List<ArticleInfo> aiList = basicDao.search(ArticleInfo.class,con,"addUser");		
		return aiList;
	}	
	/**
	 * 
	  * @方法名  showArticleInfoList 
	  * @功能描述  分页获取文章
	  * @编码人   吕凯
	  * @编码时间  2012-10-19 上午10:17:56 
	  * @输入参数   @param type
	  * @输入参数   @param request 
	  * @返回值  void
	 */
	public void getArticleInfoList(int type,int currentPage,int pageSize,HttpServletRequest request,Condition con) {
		System.out.println(currentPage+" "+currentPage+" "+pageSize);
//		Map<String, Object> map = new HashMap<String, Object>();
//		List<ArticleInfo> okList = new ArrayList<ArticleInfo>();	// 存放已关联部门信息对象的用户列表
		try {
			// 获取文章列表
//			int count = basicDao.searchCount(ArticleInfo.class);
//			PageController pc = new PageController(count,currentPage,pageSize);
//			if(count ==0)
//				pc.setData(new ArrayList<ArticleInfo>());
//			else{
//				List<ArticleInfo> aiList = basicDao.searchByPage(ArticleInfo.class,currentPage,pageSize,"addDate");
//				for(ArticleInfo ai:aiList){
//					basicDao.findLink(ai, "addUser");
//					okList.add(ai);
//				}
//				pc.setData(aiList);
//			}
//			request.setAttribute("pc", pc);			
//			List<ArticleInfo> aiList = basicDao.search(ArticleInfo.class,Cnd.orderBy().desc("addDate"));
//			for(ArticleInfo ai:aiList){
//				basicDao.findLink(ai, "addUser");
//				okList.add(ai);
//			}
//			request.setAttribute("articleInfoList", okList);	
			QueryResult qr = basicDao.searchByPageData(ArticleInfo.class,currentPage,pageSize,con,"addUser");
			request.setAttribute("qr", qr);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", "getFailure");
		}
	}
	
	/**
	 * 
	  * @方法名  getArticleInfo 
	  * @功能描述  获取信息
	  * @编码人   吕凯
	  * @编码时间  2012-10-19 下午04:09:20 
	  * @输入参数   @param id
	  * @输入参数   @param request 
	  * @返回值  void
	 */
	public void getArticleInfo(int id,HttpServletRequest request) {
		ArticleInfo data = null;
		try {
			ArticleInfo ai = basicDao.find(id,ArticleInfo.class);
			data = basicDao.findLink(ai, "attachments");
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.setAttribute("obj", data);
	}
	
	
	/**
	 * 
	  * @方法名  addArticleInfoList 
	  * @功能描述  新增稿件 http://localhost:8080/nutztest/articleinfo/addArticleInfoList.shtml
	  * @编码人   吕凯
	  * @编码时间  2012-10-19 下午04:08:36 
	  * @输入参数   @param ai
	  * @输入参数   @param request
	  * @输入参数   @param es
	  * @输入参数   @return 
	  * @返回值  ArticleInfo
	 */
	
	public ArticleInfo addArticleInfoList(ArticleInfo ai,HttpServletRequest request,Errors es) {
		ArticleInfo returnAi = null;
		System.out.println("^^123");		
		 System.out.println("=="+es.errorCount()+" "+es.getErrorsMap());
		ai.setAddDate(new Date());
		ai.setAddIp(request.getRemoteAddr());
		ai.setAddUserId(1);
		String[] access = request.getParameterValues("accessoryid");
		List<ArticleAttachment> atts = new ArrayList<ArticleAttachment>(); 
		if(access != null){
			for(String ss:access){
				ArticleAttachment att = new ArticleAttachment(ss,ss,"100");
				atts.add(att);
			}			
		}
		ai.setAttachments(atts);
		System.out.println(atts.size());
		if(es.errorCount() == 0){
//			articleInfoService.dao().insert(ai);
//			returnAi = articleInfoService.dao().insertLinks(ai,"attachments");
			
			returnAi = basicDao.saveWidth(ai,"attachments");//1.b.38中有问题 不知何故
		}
			
		System.out.println(ai.getId());
		return returnAi;
	}
	/**
	 * 
	  * @方法名  editArticleInfo
	  * @功能描述  编辑稿件
	  * @编码人   吕凯
	  * @编码时间  2012-10-19 下午04:08:10 
	  * @输入参数   @param ai
	  * @输入参数   @param request
	  * @输入参数   @param es
	  * @输入参数   @return 
	  * @返回值  ArticleInfo
	 */
	public ArticleInfo editArticleInfo(ArticleInfo ai,HttpServletRequest request,Errors es) {
		ArticleInfo dataOld = basicDao.find(ai.getId(),ArticleInfo.class);
		ArticleInfo returnAi = null;
		System.out.println("^^1234");		
		System.out.println("=="+es.errorCount()+" "+es.getErrorsMap());
		String[] access = request.getParameterValues("accessoryid");
		List<ArticleAttachment> atts = new ArrayList<ArticleAttachment>(); 
		if(access != null){
			for(String ss:access){
			 	ArticleAttachment att = new ArticleAttachment(ss,ss,"100");
				atts.add(att);
			}			
		}
		dataOld.setTitle(ai.getTitle());
		dataOld.setContent(ai.getContent());
		dataOld.setAttachments(atts);
		System.out.println(atts.size());
		if(es.errorCount() == 0){
			basicDao.update(dataOld);
			basicDao.saveLink(dataOld,"attachments");
		}
			
		System.out.println(ai.getId());
		return returnAi;
	}
	//==============================================稿件2开始===============================================
	/**
	 * 
	  * @方法名  getArticleInfoList 
	  * @功能描述  获取所有文章 用于打印
	  * @author   吕凯  
	  * @param type
	  * @param currentPage
	  * @param pageSize
	  * @param request
	  * @param con 
	  * @return  void 
	  * @编码时间 2012-10-28 上午11:20:07
	 */
	public List<Article> getArticleList(HttpServletRequest request,Condition con) {
		List<Article> aiList = basicDao.search(Article.class,con,"addUser");		
		return aiList;
	}
	/**
	 * 
	  * @方法名  showArticleInfoList 
	  * @功能描述  分页获取文章
	  * @编码人   吕凯
	  * @编码时间  2012-10-19 上午10:17:56 
	  * @输入参数   @param type
	  * @输入参数   @param request 
	  * @返回值  void
	 */
	public void getArticleList(int currentPage,int pageSize,HttpServletRequest request,Condition con) {
		System.out.println(currentPage+" "+currentPage+" "+pageSize);
		try {
			// 获取文章列表
			QueryResult qr = basicDao.searchByPageData(Article.class,currentPage,pageSize,con,"addUser");
			request.setAttribute("qr", qr);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", "getFailure");
		}
	}
	/**
	 * 
	  * @方法名  addArticleInfoList 
	  * @功能描述  新增稿件 http://localhost:8080/nutztest/articleinfo/addArticleInfoList.shtml
	  * @编码人   吕凯
	  * @编码时间  2012-10-19 下午04:08:36 
	  * @输入参数   @param ai
	  * @输入参数   @param request
	  * @输入参数   @param es
	  * @输入参数   @return 
	  * @返回值  ArticleInfo
	 */
	
	public Article addArticleInfoList(Article ai,int[] ids,HttpServletRequest request,Errors es) {
		Article returnAi = null;
		System.out.println("^^123");		
		 System.out.println("=="+es.errorCount()+" "+es.getErrorsMap());
		ai.setCreateDate(new Date());
		ai.setModifyDate(ai.getModifyDate()==null?new Date():ai.getModifyDate());
		ai.setAuthor(1);
		String[] access = request.getParameterValues("accessoryid");
		List<ArticleFile> atts = new ArrayList<ArticleFile>(); 
		if(access != null){
			for(String ss:access){
				ArticleFile att = new ArticleFile();
				att.setName(ss);
				att.setFilePath("articleinfo/"+ss);
				atts.add(att);
			}			
		}
		ai.setFiles(atts);
		
		List<NavModel> list = basicDao.searchByIds(NavModel.class, ids, "id");
		ai.setNav(list);
		
		System.out.println(atts.size());
		if(es.errorCount() == 0){
//			articleInfoService.dao().insert(ai);
//			returnAi = articleInfoService.dao().insertLinks(ai,"attachments");
			
			returnAi = basicDao.saveWidth(ai,"files");//1.b.38中有问题 不知何故
		}
		basicDao.saveRelation(ai, "nav");	
		System.out.println(ai.getId());
		return returnAi;
	}
	/**
	 * 
	  * @方法名  getArticle
	  * @功能描述  获取稿件信息
	  * @编码人   吕凯
	  * @编码时间  2012-10-19 下午04:09:20 
	  * @输入参数   @param id
	  * @输入参数   @param request 
	  * @返回值  void
	 */
	public void getArticle(int id,HttpServletRequest request) {
		Article data = null;
		try {
			Article ai = basicDao.find(id,Article.class);
//			data = basicDao.findLink(ai, "addUser");
			data = basicDao.findLink(ai, "nav");
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.setAttribute("obj", data);
	}
	/**
	 * 
	  * @方法名  editArticleInfo
	  * @功能描述  编辑稿件
	  * @编码人   吕凯
	  * @编码时间  2012-10-19 下午04:08:10 
	  * @输入参数   @param ai
	  * @输入参数   @param request
	  * @输入参数   @param es
	  * @输入参数   @return 
	  * @返回值  ArticleInfo
	 */
	public Article editArticle(Article ai,int[] ids,HttpServletRequest request,Errors es) {
		Article dataOld = basicDao.find(ai.getId(),Article.class);
		Article returnAi = null;
		ai.setModifyDate(ai.getModifyDate()==null?new Date():ai.getModifyDate());
		ai.setCreateDate(dataOld.getCreateDate());
		ai.setAuthor(dataOld.getAuthor());
		String[] access = request.getParameterValues("accessoryid");
		List<ArticleFile> atts = new ArrayList<ArticleFile>(); 
		if(access != null){
			for(String ss:access){
			 	ArticleFile att = new ArticleFile();
			 	atts.add(att);
			}			
		}
		ai.setFiles(atts);
		
		List<NavModel> list = basicDao.searchByIds(NavModel.class, ids, "id");
		ai.setNav(list);
		
		System.out.println(atts.size());
		if(es.errorCount() == 0){
			basicDao.update(ai);
			basicDao.saveLink(ai,"files");
			basicDao.clearRelation(ai, "nav");
			basicDao.saveRelation(ai, "nav");	
		}
			
		System.out.println(ai.getId());
		return returnAi;
	}
	//==============================================稿件2结束===============================================
}
